slint::include_modules!();

mod worker;
use anyhow::Error;
use tracing::info;

#[tokio::main]
async fn main() {
    let time_format = time::format_description::parse(
        "[year]-[month padding:zero]-[day padding:zero] [hour]:[minute]:[second].[subsecond digits:5]",
    )
    .unwrap();

    // let log_file = std::fs::File::create("wms-installer.log").unwrap();
    let offset = time::macros::offset!(+8);
    tracing_subscriber::fmt()
        .with_target(false)
        .with_timer(tracing_subscriber::fmt::time::OffsetTime::new(
            offset,
            time_format,
        ))
        // .with_writer(log_file)
        .with_ansi(false)
        .init();

    run().await.unwrap();
}

async fn run() -> Result<(), Error> {
    let ui = AppWindow::new()?;
    let worker = worker::Worker::new(&ui);

    let ui_weak = ui.as_weak();

    ui.on_quit({
        let chn = worker.channel.clone();
        move || {
            info!("quit signal");
            let chn = chn.clone();
            chn.send(worker::Message::Quit).unwrap();
            std::process::exit(0);
        }
    });

    ui.run()?;
    worker.join().await?;

    Ok(())
}